<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommonController
{

	public $GoodId,$Color,$Version,$GoodcategoryId,$GoodName,$Price,$Number,$Status,$KeyWords,$PictureUrl,$Description,$IsBest;

	public function index(){
		$this->display();
	}

	/**
	 * @return   void
	 */
	public function checkGoods()
	{

		$status=I('get.Status',0);
		$isbest=I('get.Is_best',0);
		$order=I('get.order','GoodId asc');
		// TODO: implement
		//获得请求参数
		$data['GoodcategoryId'] = I('get.GoodcategoryId',0,'int');
		//获得分类列表
		$data['goodcategory'] = D('goodcategory')->getList();
		//获得商品列表
		$data['goods'] = D('goods')->getList(
		//待查询字段
		'GoodId,Color,Version,goods.GoodcategoryId,CategoryName,GoodName,Status,Price,Number,Is_best',
		//查询条件
		array('goods.GoodcategoryId'=>$data['GoodcategoryId'],'Status'=>$status,'Is_best'=>$isbest),$order);
		//视图

		$this->assign('statu',$status);
		$this->assign('isbest',$isbest);
		$this->assign($data);
		$this->display('index');
	}

	/**
	 * @return   void
	 */
	public function addGoods()
	{
		// TODO: implement
		if(IS_POST){
			$data['Color']=I('post.Color','');
			$data['Version']=I('post.Version','');
			$data['GoodcategoryId']=I('post.GoodcategoryId');
			$data['GoodName']=I('post.GoodName');
			$data['Price']=I('post.Price',0);
			$data['Number']=I('post.Number',0);
			$data['Status']=I('post.Status',2);
			$data['KeyWords']=I('post.KeyWords');

			$data['Description']=I('post.Description','','');
			$data['Is_best']=I('post.Is_best',2);
			$rst = M("goods")->add($data);

			if($rst===false){
				$this->error('添加商品失败');
			}

			if(!empty($_FILES['img']['size'])){  //上传图片并保存路径到goods表里
				$rste = D('Goods')->uploadThumb($rst,$data['Color'],$data['Version']);
				if($rste!==true){
					$this->error($rste,U('Goods/modifyGoods','goodids='.$rst.'@@@'.$data['Color'].'@@@'.$data['Version']));
				}
			}
			$attr = M("goods_has_goodattribute"); // 实例化对象

			$attrdata['goods_GoodId'] = $rst;
			$attrdata['goods_Color'] = I('post.Color','');
			$attrdata['goods_Version'] =I('post.Version','');
			$goodattribute_GoodAttributeid=I('post.goodattribute_GoodAttributeid');
			$AttributeValue=I('post.AttributeValue',null);
			for($i=0;$i<count($goodattribute_GoodAttributeid);$i++){
				if($AttributeValue==null)break;		//如果属性为空，则直接跳出添加属性的循环
				$attrdata['goodattribute_GoodAttributeid']=$goodattribute_GoodAttributeid[$i];
				$attrdata['AttributeValue']=$AttributeValue[$i];
				$attrrst=$attr->add($attrdata);
				if($attrrst===false){
					$where['GoodId']=$rst;
					$where['Color']=$data['Color'];
					$where['Version']=$data['Version'];
					M('goods')->where($where)->delete();
					$this->error('添加商品失败！'.$where['Color']);
				}
			}
			echo "<script language='javascript' type='text/javascript'>alert('添加商品成功')</script>";
			$url = __CONTROLLER__."/checkGoods";
			echo "<script language='javascript' type='text/javascript'>window.location.href='$url'</script>";
			return;
		}
		//视图
		$data['goodcategory'] = D('goodcategory')->getList(); //获得分类列表，排序好了的，无缩进
		$this->assign($data);
		$this->display('add');
	}

	/**
	 * @return   void
	 */
	public function modifyGoods()
	{
		// TODO: implement

		$subwherex=explode('@@@', $_GET['goodids']);
		$subwhere['GoodId']=$subwherex[0];
		$subwhere['Color']=$subwherex[1];
		$subwhere['Version']=$subwherex[2];
		if(IS_POST){
			$data['Color']=I('post.Color','?');
			$data['Version']=I('post.Version','?');
			$data['GoodcategoryId']=I('post.GoodcategoryId');
			$data['GoodName']=I('post.GoodName');
			$data['Price']=I('post.Price',0);
			$data['Number']=I('post.Number',0);
			$data['Status']=I('post.Status',2);
			$data['KeyWords']=I('post.KeyWords');
			$data['Description']=I('post.Description','','');
			$data['Is_best']=I('post.Is_best',2);




			$attrdataAttributeValue=I('post.AttributeValue',array());
			$attrdatagoodattribute_GoodAttributeid=I('post.goodattribute_GoodAttributeid',array());
			//删除原来的商品属性，让商品的color和version可以更改，不受外键干扰
			$attrwhere['goods_GoodId']=$subwherex[0];
			$attrwhere['goods_Color']=$subwherex[1];
			$attrwhere['goods_Version']=$subwherex[2];
			$attr = M("goods_has_goodattribute"); // 实例化对象
			$attr->where($attrwhere)->delete();
			//----------------------------------------------------
			//修改商品属性
			$rst = M("goods")->where($subwhere)->save($data);
			if($rst===false){
				$this->error('修改商品失败');
			}

			if(!empty($_FILES['img']['size'])){  //上传图片并保存路径到goods表里
				$rste = D('goods')->uploadThumb($subwhere['GoodId'],$data['Color'],$data['Version']);
				if($rste!==true){
					$this->error($rste,U('Goods/modifyGoods','goodids='.$rst.'@@@'.$data['Color'].'@@@'.$data['Version']));
				}
			}

			//	--------------------------------------------------
			//添加新的商品属性（使用新的商品主键color和version）
			$attrdata['goods_GoodId']=$subwhere['GoodId'];
			$attrdata['goods_Color']=$data['Color'];
			$attrdata['goods_Version']=$data['Version'];
			for($i=0;$i<count($attrdatagoodattribute_GoodAttributeid);$i++){
				$attrdata['goodattribute_GoodAttributeid']=$attrdatagoodattribute_GoodAttributeid[$i];
				$attrdata['AttributeValue']=$attrdataAttributeValue[$i];
				$attrrst=$attr->add($attrdata);
				if($attrrst===false){
					$this->error('添加商品属性失败！');
				}
			}

			//

			echo "<script language='javascript' type='text/javascript'>alert('修改商品成功')</script>";
			$url = __CONTROLLER__."/checkGoods";
			echo "<script language='javascript' type='text/javascript'>window.location.href='$url'</script>";
			return;
		}
		$data=M('goods')->where($subwhere)->find();

		$attrwhere['goods_GoodId']=$subwhere['GoodId'];
		$attrwhere['goods_Color']=$subwhere['Color'];
		$attrwhere['goods_Version']=$subwhere['Version'];
		$goodattribute=M('goods_has_goodattribute')->where($attrwhere)->select();

		$goodcategory=D('Goodcategory')->getList();
		$this->assign("goodattribute",$goodattribute);
		$this->assign("goodids",$_GET['goodids']);
		$this->assign("goodcategory",$goodcategory);
		$this->assign("data",$data);
		$this->display('update');
	}

	//-------------------------------------------------------------------------
	public function addCopy(){
		$subwherex=explode('@@@', $_GET['goodids']);
		$subwhere['GoodId']=$subwherex[0];
		$subwhere['Color']=$subwherex[1];
		$subwhere['Version']=$subwherex[2];
		if(IS_POST){
			$data['GoodId']=$subwhere['GoodId'];
			$data['Color']=I('post.Color','?');
			$data['Version']=I('post.Version','?');
			$data['GoodcategoryId']=I('post.GoodcategoryId');
			$data['GoodName']=I('post.GoodName');
			$data['Price']=I('post.Price',0);
			$data['Number']=I('post.Number',0);
			$data['Status']=I('post.Status',2);
			$data['KeyWords']=I('post.KeyWords');
			$data['Description']=I('post.Description','','');
			$data['Default']=0;
			$data['Is_best']=I('post.Is_best',2);
			$attrdataAttributeValue=I('post.AttributeValue',array());
			$attrdatagoodattribute_GoodAttributeid=I('post.goodattribute_GoodAttributeid',array());


			//----------------------------------------------------
			//修改商品属性
			$rst=M('goods')->data($data)->add();

			if($rst===false){
				$this->error('添加商品失败');
			}

			if(!empty($_FILES['img']['size'])){  //上传图片并保存路径到goods表里
				$rste = D('goods')->uploadThumb($rst,$data['Color'],$data['Version']);
				if($rste!==true){
					$this->error($rste,U('Goods/modifyGoods','goodids='.$rst.'@@@'.$data['Color'].'@@@'.$data['Version']));
				}
			}
			//	--------------------------------------------------
			//添加新的商品属性（使用新的商品主键color和version）
			$attrdata['goods_GoodId']=$data['GoodId'];
			$attrdata['goods_Color']=$data['Color'];
			$attrdata['goods_Version']=$data['Version'];
			$attr=M('goods_has_goodattribute');
			for($i=0;$i<count($attrdatagoodattribute_GoodAttributeid);$i++){
				$attrdata['goodattribute_GoodAttributeid']=$attrdatagoodattribute_GoodAttributeid[$i];
				$attrdata['AttributeValue']=$attrdataAttributeValue[$i];
				$attrrst=$attr->add($attrdata);
				if($attrrst===false){
					$this->error('添加商品属性失败！');
				}
			}

			echo "<script language='javascript' type='text/javascript'>alert('新增商品成功')</script>";
			$url = __CONTROLLER__."/checkGoods";
			echo "<script language='javascript' type='text/javascript'>window.location.href='$url'</script>";
			return;
		}
		$data=M('goods')->where($subwhere)->find();
		$attrwhere['goods_GoodId']=$subwhere['GoodId'];
		$attrwhere['goods_Color']=$subwhere['Color'];
		$attrwhere['goods_Version']=$subwhere['Version'];
		$goodattribute=M('goods_has_goodattribute')->where($attrwhere)->select();

		$goodcategory=D('Goodcategory')->getList();
		$this->assign("goodattribute",$goodattribute);
		$this->assign("goodids",$_GET['goodids']);
		$this->assign("goodcategory",$goodcategory);
		$this->assign("data",$data);
		$this->display('addcopy');
	}





	/**
	 * @return   void
	 */
	public function deleteGoods()
	{
		// TODO: implement
		$subwherex=explode('@@@',I('get.goodids'));
		$attrwhere['goods_GoodId']=$where['GoodId']=$subwherex[0];
		$attrwhere['goods_Color']=$where['Color']=$subwherex[1];
		$attrwhere['goods_Version']=$where['Version']=$subwherex[2];

		M('goods_has_goodattribute')->where($attrwhere)->delete();
		D('Goods')->delImage($where['GoodId'],$where['Color'],$where['Version']);
		M('goods')->where($where)->delete();
			
		if($res === false){
			$this->error("删除商品失败");
		}
		header('location:'.__CONTROLLER__.'/recycleIndex/GoodcategoryId/'.I('get.GoodcategoryId',0));

	}


	public function delSelect(){
		// TODO: implement
		$where=array();
		$attrwhere=array();
		$subwhere=array();
		foreach ($_POST['goodids'] as $v){
			$subwherex=explode('@@@', $v);
			$attrsubwhere['goods_GoodId']=$subwhere['GoodId']=$subwherex[0];
			$attrsubwhere['goods_Color']=$subwhere['Color']=$subwherex[1];
			$attrsubwhere['goods_Version']=$subwhere['Version']=$subwherex[2];
			array_push($where, $subwhere);
			array_push($attrwhere, $attrsubwhere);
			$subwhere=array();
			$attrsubwhere=array();
		}

		foreach($attrwhere as $v){
			M('goods_has_goodattribute')->where($v)->delete();
		}

		foreach ($where as $a){
			D('Goods')->delImage($a['GoodId'],$a['Color'],$a['Version']);
			M('goods')->where($a)->delete();
		}
		if($res === false){
			$this->error("删除商品失败");
			//			echo $_POST['goodids'][0]['GoodId'];
			//			return;
		}
		header('location:'.__CONTROLLER__.'/recycleIndex/GoodcategoryId/'.I('post.GoodcategoryId',0));
	}


	public function xiajiaSelect(){
		// TODO: implement
		$where=array();
		$subwhere=array();
		foreach ($_POST['goodids'] as $v){
			$subwherex=explode('@@@', $v);
			$subwhere['GoodId']=$subwherex[0];
			$subwhere['Color']=$subwherex[1];
			$subwhere['Version']=$subwherex[2];
			array_push($where, $subwhere);
			$subwhere=array();
		}
		foreach ($where as $v){
			$res=M('goods')->where($v)->setField('Status',2);
		}
		if($res === false){
			$this->error("下架商品失败");
		}
		header('location:'.__CONTROLLER__.'/checkGoods/GoodcategoryId/'.I('post.GoodcategoryId',0).
		'Status/'.I('post.Status',0).'Is_best/'.I('post.Is_best',0));
	}


	public function xiajiaGoods(){
		// TODO: implement
		$where['GoodId']=I('get.GoodId',0);
		$where['Color']=I('get.Color',0);
		$where['Version']=I('get.Version',0);
		if($where['GoodId']==0)unset($where['GoodId']);
		$status=I('get.Status',2);
		if($status==1)$status=2;
		else $status=1;
		$res=M('goods')->where($where)->setField('Status',$status);
		if($res === false){
			$this->error("修改商品上下架失败");
		}
		header('location:'.__CONTROLLER__.'/checkGoods/GoodcategoryId/'.I('get.GoodcategoryId',0).'/Status/'.$status.'/Is_best/'.I('get.Is_best',0));
	}


	public function tuijianGoods(){
		// TODO: implement
		$where['GoodId']=I('get.GoodId',0);
		$where['Color']=I('get.Color',0);
		$where['Version']=I('get.Version',0);
		if($where['GoodId']==0)unset($where['GoodId']);
		$isbest=I('get.Is_best',2);
		if($isbest==1)$isbest=2;
		else $isbest=1;
		$res=M('goods')->where($where)->setField('Is_best',$isbest);
		if($res === false){
			$this->error("修改商品推荐失败");
		}
		header('location:'.__CONTROLLER__.'/checkGoods/GoodcategoryId/'.I('get.GoodcategoryId',0).'/Status/'.I('get.Status',0).'/Is_best/'.$isbest);
	}




	public function huanyuanselect(){
		// TODO: implement
		$where=array();
		$subwhere=array();
		foreach ($_POST['goodids'] as $v){
			$subwherex=explode('@@@', $v);
			$subwhere['GoodId']=$subwherex[0];
			$subwhere['Color']=$subwherex[1];
			$subwhere['Version']=$subwherex[2];
			array_push($where, $subwhere);
			$subwhere=array();
		}
		foreach ($where as $v){
			$res=M('goods')->where($v)->setField('Status',1);
		}
		if($res === false){
			$this->error("还原商品失败");
		}
		header('location:'.__CONTROLLER__.'/checkGoods/GoodcategoryId/'.I('post.GoodcategoryId',0).
		'Status/'.I('post.Status',0).'Is_best/'.I('post.Is_best',0));
	}


	public function recycleIndex(){
		// TODO: implement
		//获得请求参数
		$data['GoodcategoryId'] = I('get.GoodcategoryId',0,'int');
		//获得分类列表
		$data['goodcategory'] = D('goodcategory')->getList();
		//获得商品列表
		$data['goods'] = D('goods')->getList(
		//待查询字段
		'GoodId,Color,Version,goods.GoodcategoryId,CategoryName,GoodName,Status',
		//查询条件
		array('goods.GoodcategoryId'=>$data['GoodcategoryId'],'Status'=>2));
		//视图
		$this->assign($data);
		$this->display('recycle');
	}


	public function checkGoodAjax(){
		if(IS_POST){
			$where['GoodId']=I('post.GoodId',null);
			if($where['GoodId']==null)unset($where);
			$good=M('goods')->where($where)->select();
			$this->ajaxReturn($good);	//返回到ajax中是msg
		}
	}
	
	
	
	
	/**
	 * 导出goods数据库表到excel
	 */
	public function goods2Excel(){
		//获取你要导出的数据，你要获取的到数据库的数据
		$data=M('goods')->field('GoodId,Color,Version,GoodName,Price,Number,Status,KeyWords,Description,Is_best')->limit(520)->select();
		//设置要导出excel的表头
		$fileheader=array("商品编号","商品颜色","商品型号","商品名称","单价","库存量","上下架状态","关键字","描述","推荐");
		$excelname="商品详细信息";
		exportExcel($data,$excelname,$fileheader,'Sheet1');
	}
	
}

?>